package cn.yzgy.yzgyoa.common.controller;


import cn.yzgy.yzgyoa.common.annotation.ApiDesc;
import cn.yzgy.yzgyoa.common.annotation.Syslog;
import cn.yzgy.yzgyoa.common.crsp.Urmf;
import cn.yzgy.yzgyoa.common.dto.SysDictValueDto;
import cn.yzgy.yzgyoa.common.entity.SysDictValue;
import cn.yzgy.yzgyoa.common.service.ISysDictValueService;
import cn.yzgy.yzgyoa.ucenter.entity.UserInfoDetail;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yqsen
 * @since 2021-07-14
 */
@Slf4j
@Syslog
@RestController
@RequestMapping("/common/sys-dict-value")
public class SysDictValueController extends BaseController {

    private ISysDictValueService sysDictValueService;

    public SysDictValueController(ISysDictValueService sysDictValueService) {
        this.sysDictValueService = sysDictValueService;
    }

    @ApiDesc("数据字典值列表")
    @GetMapping("getAllDictValue")
    @RequiresPermissions("api:dict:dictValue:getAllDictValue")
    public Urmf getAllDictValue(@RequestParam Integer page, @RequestParam Integer size, SysDictValueDto queryDto) {
        IPage<SysDictValueDto> dictPage = sysDictValueService.getAllDictValue(new Page<>(page, size), queryDto);
        return Urmf.success(getCurrentMethodName(), dictPage);
    }
    @ApiDesc("单个数据字典值列表")
    @GetMapping("getDictValue")
    @RequiresPermissions("api:dict:dictValue:getDictValue")
    public Urmf getDictValue(@RequestParam String dictCode) {
        return Urmf.success(getCurrentMethodName(), sysDictValueService.getDictValue(dictCode));
    }

    @ApiDesc("新增数据字典值类型")
    @PostMapping("createDictValue")
    @RequiresPermissions("api:dict:dictValue:createDictValue")
    public Urmf createDictValue(@RequestBody SysDictValue queryDto) {
        UserInfoDetail userInfoDetail = getCurrentUser();
        boolean state = sysDictValueService.createDictValue(userInfoDetail, queryDto);
        if(state){
            return Urmf.success(getCurrentMethodName(), "新增数据字典值成功！");
        }else{
            return Urmf.fail(getCurrentMethodName(), "新增数据字典值失败！");
        }

    }


    @ApiDesc("修改数据字典值")
    @PutMapping("updateDictValue")
    @RequiresPermissions("api:dict:dictValue:updateDictValue")
    public Urmf updateDictValue(@RequestBody SysDictValue queryDto) {
        UserInfoDetail userInfoDetail = getCurrentUser();
        int i = sysDictValueService.updateDictValue(userInfoDetail,queryDto);
        if(i>0){
            return Urmf.success(getCurrentMethodName(), "修改数据成功!");
        }else{
            return Urmf.fail(getCurrentMethodName(), "修改数据失败!");
        }
    }

    @ApiDesc("删除数据字典值")
    @DeleteMapping("delDictValue")
    @RequiresPermissions("api:dict:dictValue:delDictValue")
    public Urmf delDictValue(@RequestBody SysDictValueDto queryDto) {
        boolean b = sysDictValueService.removeById(queryDto.getId());
        if(b){
            return Urmf.success(getCurrentMethodName(),"删除数据成功！");
        }else{
            return Urmf.fail(getCurrentMethodName(),"删除数据失败！");
        }
    }
}
